
// Main header
#include "../Main.h"

//My header
#include "DialogPanel.h"

// Fridge headers
#include "../Controller/Controller_Main.h"
#include "../Input.h"
#include "../MathsHelper.h"
#include "../TextureManager.h"
#include "../Timer.h"

static const u_int uDIALOG_PANEL_TITLEBAR_COL1 = 0xFFDFDFDF;
static const u_int uDIALOG_PANEL_TITLEBAR_COL2 = 0xFFEFEFEF;
static const u_int uDIALOG_PANEL_TITLEBAR_BORDER = 3;
static const u_int uDIALOG_PANEL_TITLEBAR_HEIGHT = 24;
static const float fDIALOG_PANEL_TITLEBAR_HOVER_SPEED = 4.0f;

static const u_int uDIALOG_PANEL_SHADOW_COL = 0x44000000;

DialogPanel::DialogPanel( Widget* pxParent ) : Panel( pxParent )
, m_bHasMoveBar( false )
, m_uMouseDownX( 0 )
, m_uMouseDownY( 0 )
, m_bMoving( false )
, m_fHoverTimer( 0.0f )
{
}

void DialogPanel::Update()
{
	Input* pxInputHandler = Controller_Main::GetInputHandler();

	Widget::Update();
	
	if( m_bHasMoveBar )
	{
		const bool bMouseOverMoveBar = MouseInAreaOfWidget( 0, 0, GetResizedWidth(), uDIALOG_PANEL_TITLEBAR_HEIGHT );
		if( bMouseOverMoveBar )
		{
			m_fHoverTimer += Timer::GetFrameTime() * fDIALOG_PANEL_TITLEBAR_HOVER_SPEED;
			if( m_fHoverTimer > 1.0f )
			{
				m_fHoverTimer = 1.0f;
			}
		}
		else
		{
			m_fHoverTimer -= Timer::GetFrameTime() * fDIALOG_PANEL_TITLEBAR_HOVER_SPEED;
			if( m_fHoverTimer < 0.0f )
			{
				m_fHoverTimer = 0.0f;
			}
		}
		
		if( m_bMoving )
		{
			m_fHoverTimer = 1.0f;

			if( pxInputHandler->MouseButtonReleased( INPUT_MOUSE_BUTTON_LEFT ) )
			{
				m_bMoving = false;
			}
			else
			{
				SetPosition( pxInputHandler->MousePositionX() - m_uMouseDownX, pxInputHandler->MousePositionY() - m_uMouseDownY  );
			}
		}
		else
		{
			if( bMouseOverMoveBar && pxInputHandler->MouseButtonPressed( INPUT_MOUSE_BUTTON_LEFT ) )
			{
				m_uMouseDownX = pxInputHandler->MousePositionX() - GetResizedX();
				m_uMouseDownY = pxInputHandler->MousePositionY() - GetResizedY();
				m_bMoving = true;
			}
		}
	}
}

void DialogPanel::Render()
{
	if( !m_bVisible )
	{
		return;
	}
	
	TextureManager* pxTextureManager = Controller_Main::GetTextureManager();

	if( pxTextureManager )
	{
		const int iTextureID = pxTextureManager->GetTextureID( GetStringHash( "Shadow256" ) );

		glBindTexture( GL_TEXTURE_2D, iTextureID );

		glBegin( GL_QUADS );

		glColor4ubv( reinterpret_cast<const GLubyte*>( &uDIALOG_PANEL_SHADOW_COL ) );

		const u_int uShadowSize = 16;

		glTexCoord2f( 0.0f, 0.0f );
		glVertex2i( GetResizedX() - ( uShadowSize / 2 ), GetResizedY() - u_int( uShadowSize * 0.66f ) );
		glTexCoord2f( 0.0f, 1.0f );
		glVertex2i( GetResizedX() - uShadowSize, GetResizedY() + GetResizedHeight() + uShadowSize );
		glTexCoord2f( 1.0f, 1.0f );
		glVertex2i( GetResizedX() + GetResizedWidth() + uShadowSize, GetResizedY() + GetResizedHeight() + uShadowSize );
		glTexCoord2f( 1.0f, 0.0f );
		glVertex2i( GetResizedX() + GetResizedWidth() + ( uShadowSize / 2 ), GetResizedY() - u_int( uShadowSize * 0.66f ) );

		glEnd();
	}

	Panel::Render();

	if( m_bHasMoveBar )
	{
		const u_int uCol1 = MathsHelper::InterpolateColour( uDIALOG_PANEL_TITLEBAR_COL1, 0x00000000, m_fHoverTimer * 0.2f );
		const u_int uCol2 = MathsHelper::InterpolateColour( uDIALOG_PANEL_TITLEBAR_COL2, 0x00AAAAAA, m_fHoverTimer * 0.2f );

		glBegin( GL_QUADS );

		glColor4ubv( reinterpret_cast<const GLubyte*>( &uCol1 ) );
		glTexCoord2f( 0.0f, 0.0f );
		glVertex2i( GetResizedX() + uDIALOG_PANEL_TITLEBAR_BORDER, GetResizedY() + uDIALOG_PANEL_TITLEBAR_BORDER );

		glColor4ubv( reinterpret_cast<const GLubyte*>( &uCol2 ) );
		glTexCoord2f( 0.0f, 1.0f );
		glVertex2i( GetResizedX() + uDIALOG_PANEL_TITLEBAR_BORDER, GetResizedY() + uDIALOG_PANEL_TITLEBAR_HEIGHT - uDIALOG_PANEL_TITLEBAR_BORDER * 2 );
	
		glColor4ubv( reinterpret_cast<const GLubyte*>( &uCol2 ) );
		glTexCoord2f( 1.0f, 1.0f );
		glVertex2i( GetResizedX() + GetResizedWidth() - uDIALOG_PANEL_TITLEBAR_BORDER, GetResizedY() + uDIALOG_PANEL_TITLEBAR_HEIGHT - uDIALOG_PANEL_TITLEBAR_BORDER * 2 );

		glColor4ubv( reinterpret_cast<const GLubyte*>( &uCol1 ) );
		glTexCoord2f( 1.0f, 0.0f );
		glVertex2i( GetResizedX() + GetResizedWidth() - uDIALOG_PANEL_TITLEBAR_BORDER, GetResizedY() + uDIALOG_PANEL_TITLEBAR_BORDER );

		glEnd();
	}
}

// eof
